// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var leftMenu = require("../leftMenu/leftMenu");
	var topMenu = require("../topMenu/topMenu");
	var router = require("../hash/pageRouter");
	var css_financeManage = require("./pickupList.css");
	var footer = require("../footer/footer_main");
	var css_detail_info = require("./financingDetailInfo.css");
	var html_cancelApplication = require("./html_cancelApplication.html");
	var dialog = require("dialog");
	var validate = require("validate");
	var constant = require("constant")
	var databind = require("databind");
	var tools = require("tools");
	var pickupId;
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var left;
	var financeState;
	var financeId;
	var financeNumber;
	var dataFinancingDetail;
	var init = function(page) {
		footer.init();
		$("#footer").removeClass("none");
		leftMenu.init(5);
		topMenu.init(1);
		$(".footer").removeClass("none");
		$(".pickupApplicationManage").addClass("leftMenu-active");
		$("#leftMenu6 .leftMenu li:first-child").addClass("leftMenu-open");
		inquirePickupApplicationPageList();
		initEvent();
		router.init();

	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}
		//点击首页
		$(".platform_main").unbind("click").click(function() {
			top.location.href = "../main/platform_main.html";
		});

		$("#pickupList .searchButon").unbind("click").click(function() {
			pageIndex = 1;
			if ($("#pickupList .searchStatus").val() == 1) {
				inquirePickupApplicationPageList1();
			} else {
				inquirePickupApplicationPageList();
			}
		});
	};

	//提货申请列表
	var inquirePickupApplicationPageList = function() {
		var productType=$("#pickupList select[name=productType]").val();
		var financeNumber = $("#financeNumber").val();
		var bulkstockName = $("#bulkstockName").val();
		var pickupMethod =  $(".pickupMethod").val();
		base.fnAjax({
			action: "inquireNormalPickupApplicationPageList.json",
			oData: {
				/*pickupId: $("#pickups").val().split(','),*/
				pageIndex: pageIndex,
				pageSize: pageSize,
				pickupMethod:pickupMethod,
				bulkstockName:bulkstockName,
				waybillNumber: $("#pickupList .waybillNumber").val(),
				caseNumber: $("#pickupList .caseNumber").val(),
				enterpriseName: $("#pickupList .enterpriseName").val(),
				productType: productType,
				financeNumber: financeNumber
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					juicer.register("getValueByKey", constant.getValueByKey);
					var tpl = $("#mainPickupListTpl").html();
					var html = juicer(tpl, data);
					$("#mainPickupListBody").html(html);
					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl(
						$("#mainPickupPagenation"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquirePickupApplicationPageList();
						});
					//详情
					$(".pickupDetail").unbind("click").click(function() {
						router.route("main/pickupDetail.html?left=" + left + "&pickupId=" + $(this).parents("tr").attr("pickupId"));
					});
					//货物一览
					$(".bulkstockListing").unbind("click").click(function() {
						router.route("bulkstockListing/accountBulkstockListing.html?financeId=" + $(this).parents("tr").attr("financeId"));
					});

					tools.changeWarningColor($("#mainPickupListBody"), "pickupApplicationCreateDatetime");

					//点击融资编号
					$(".order_num").unbind("click").click(function() {
						var financeIds = [];
						var $this = $(this).parents("tr.tr_style");
						financeIds.push($this.attr("financeId"));
						router.route("main/financingDetailInfo.html?financeId=" + financeIds, true);
					});
					
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};

	//提货申请列表
	var inquirePickupApplicationPageList1 = function() {
		var productType=$("#pickupList select[name=productType]").val();
		var financeNumber = $("#financeNumber").val();
		var bulkstockName = $("#bulkstockName").val();
		var pickupMethod =  $(".pickupMethod").val();
		base.fnAjax({
			action: "inquireNormalPickupApplicationPageList.json",
			oData: {
				pageIndex: pageIndex,
				pageSize: pageSize,
				pickupMethod:pickupMethod,
				bulkstockName:bulkstockName,
				waybillNumber: $("#pickupList .waybillNumber").val(),
				caseNumber: $("#pickupList .caseNumber").val(),
				enterpriseName: $("#pickupList .enterpriseName").val(),
				productType: productType,
				financeNumber: financeNumber
				
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $("#mainPickupListTpl").html();
					var html = juicer(tpl, data);
					$("#mainPickupListBody").html(html);

					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl(
						$("#mainPickupPagenation"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquirePickupApplicationPageList1();
						});
					$(".pickupDetail").unbind("click").click(function() {
						router.route("main/pickupDetail.html?left=" + left + "&pickupId=" + $(this).parents("tr").attr("pickupId"));
					});

					$.each($(".applicationDateTime3"), function() {
						var msec = tools.timeCalculator(new Date($(this).attr("applicationDateTime").replace(/-/g, "/")), new Date());
						var t = msec / 3600000;
						if (t >= 3 && t < 6) {
							$(this).find("td").css({
								"background-color": "#fcfae0",
								"color": "#000"
							});
						} else if (t > 6) {
							$(this).find("td").css({
								"background-color": "#ffeeee",
								"color": "#000"
							});
						}
					});

					//点击融资编号
					$(".order_num").unbind("click").click(function() {
						var financeIds = [];
						var $this = $(this).parents("tr.tr_style");
						financeIds.push($this.attr("financeId"));
						router.route("financing/financingDetailInfo.html?financeId=" + financeIds, true);
					});
					
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};
	
	return {
		init: init
	};
});